//
//  HomePageDataManager.m
//  GaoBiaoB
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "HomePageDataManager.h"
#import "Url.h"
#import "HomeCatrgeryModel.h"
#import "BannersModel.h"
#import "GuessModel.h"
#import "Nuomishuo.h"
#import "TuandanFenglei.h"
#import "TuanSmallModel.h"
@interface HomePageDataManager()

@end
@implementation HomePageDataManager

//创建数据管理类对象
+(instancetype)sharedHomeDataManger{
    static HomePageDataManager *homeData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        homeData = [[HomePageDataManager alloc]init];
        [homeData requestNetWorkCollectionShow];
        [homeData rrrequestNetWork];
    });
    return homeData;
}
-(void)requestNetWorkCollectionShow{
    NSString *httpStr = @"http://apis.baidu.com/baidunuomi/openapi/categories";
    NSString *urlStr = @"";
    Nuomishuo *nuo = [[Nuomishuo alloc]init];
    [nuo request:httpStr withHttpArg:urlStr];
    __weak typeof(nuo)temp = nuo;
    dispatch_async(dispatch_get_main_queue(), ^{
        nuo.upData = ^(){
//            NSLog(@"%@",temp.array);
            for (TuandanFenglei * k_Category in temp.array) {
                NSString *key = k_Category.cat_name;

                [self.categoryIDArray addObject:[NSString stringWithFormat:@"%@",k_Category.cat_id]];
                for (NSDictionary *small in k_Category.subcategories) {
                    TuanSmallModel *model = [TuanSmallModel new];
                    [model setValuesForKeysWithDictionary:small];
                    //要是组不存在那么重新创建
                    if (self.categoryDict[key] == nil) {
                        NSMutableArray *groupArray = [NSMutableArray array];
                        [groupArray addObject:model];
                        [self.categoryDict setObject:groupArray forKey:key];
                    }else{
                        NSMutableArray *groupArray = self.categoryDict[key];
                        [groupArray addObject:model];
                    }
                }
            }
            self.categoryNameArray =[NSMutableArray arrayWithArray:self.categoryDict.allKeys];
            self.block();
        };
    });
}
-(NSMutableDictionary *)categoryDict{
    if (_categoryDict == nil) {
        _categoryDict = [NSMutableDictionary dictionary];
    }
    return _categoryDict;
}
//猜你喜欢
-(void)rrrequestNetWork{
    
    NSString *str = kUrlGuessYouLike;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:str] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        NSArray *array = dict[@"data"][@"tuan_list"];
        for (NSDictionary *dic in array) {
            GuessModel *model = [GuessModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.guessYouLikeArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.block();
        });
    }];
    [dataTask resume];
}
-(NSMutableArray *)homeShowCategaryArray{
    if (_homeShowCategaryArray == nil) {
        _homeShowCategaryArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _homeShowCategaryArray;
}
-(NSMutableArray *)bannersArr{
    if (_bannersArr == nil) {
        _bannersArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _bannersArr;
}
-(NSMutableArray *)guessYouLikeArr{
    if (_guessYouLikeArr == nil) {
        _guessYouLikeArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _guessYouLikeArr;
}
-(NSMutableArray *)categoryIDArray{
    if (_categoryIDArray == nil) {
        _categoryIDArray =[NSMutableArray array];
    }
    return _categoryIDArray;
}
@end
